package xml.sax.defaults;

import java.io.InputStream;

import javax.faces.context.FacesContext;
import javax.servlet.ServletContext;
import javax.xml.bind.Marshaller;

import exception.IBToolsException;

import xml.sax.ClassNameLoader;
import xml.sax.JaxbUtil;

public class DefaultsXMLProcessor {
	
	private static JaxbUtil jaxbUtil;
	
	public static void utilize(){
		String name = ClassNameLoader.getPackageName(new Object() {}.getClass());
		
		jaxbUtil = new JaxbUtil(name);
		jaxbUtil.setRemoveXmlHeaderTag(true);
		jaxbUtil.addMarshallProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
	}
	
	public static IbtoolsConstants loadDefaults() throws IBToolsException{
		ServletContext servletContext = (ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext();
		String resourceName = "/WEB-INF/ibtools-config.xml";
		InputStream i = servletContext.getResourceAsStream(resourceName);
		return (IbtoolsConstants) jaxbUtil.unmarshal(i);
	}
}
